<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="38px">
        <el-form-item label="日期">
          <el-date-picker
            v-model="searchParam.planDateSearch"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            @change="getcreateTime"
            style="width: 280px !important"
          />
        </el-form-item>
        <el-form-item label="品名" prop="materialName">
          <el-input
            v-model="searchParam.materialName"
            placeholder="请输入品名"
            clearable
            style="width: 160px !important"
          />
        </el-form-item>
        <el-form-item label="批号" prop="plannedBatch">
          <el-input
            v-model="searchParam.plannedBatch"
            placeholder="请输入批号"
            style="width: 160px !important"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="startSearch"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="startReset"><i-ep-refresh />重置</el-button>

          <el-button
            type="primary"
            @click="
              useDownload(
                '/mes/messhapestatistics/export',
                '造型生产统计',
                searchParam,
                true,
                '.xls'
              )
            "
          >
            <template #icon><i-ep-download /></template>导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- <el-card shadow="never" class="mb-[10px]"></el-card> -->

    <el-card shadow="never" class="table-container relative overflow-visible!">
      <!-- <template #header>
       
      </template> -->
      <div id="descriptions-container">
        <el-descriptions
          class="mb-[12px]"
          title=""
          :column="6"
          size="default"
          border
        >
          <el-descriptions-item label="计划模数">{{
            totalData?.planModulus ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="计划重量">{{
            totalData?.planWeight ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="计划件数">{{
            totalData?.planQuantity ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="计划铁水重量">{{
            totalData?.planIronWeight ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际造型模数">{{
            totalData?.actualModelMouldQuantity ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际浇注模数">{{
            totalData?.actualPouringMouldQuantity ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="造型废型">{{
            totalData?.modelAbandon ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="浇注废型">{{
            totalData?.pouringAbandon ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际重量">{{
            totalData?.actualWeight ?? ""
          }}</el-descriptions-item>

          <el-descriptions-item label="实际件数">{{
            totalData?.actualQuantity ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际铁水重量">{{
            totalData?.actualWaterWeight ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="计划生产时长/分钟">{{
            totalData?.planProduceDuration ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际生产时长/分钟">{{
            totalData?.actualProduceDuration ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="计划包待/分钟">{{
            totalData?.packageWaitingTime ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际包待/分钟">{{
            totalData?.actualPackage ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="计划炉待/分钟">{{
            totalData?.furnaceWaitingTime ?? ""
          }}</el-descriptions-item>
          <el-descriptions-item label="实际炉待/分钟">{{
            totalData?.actualStove ?? ""
          }}</el-descriptions-item>
        </el-descriptions>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :maxHeight="tableHeight"
        border
        @selection-change="handleSelectionChange"
        @sort-change="onTableSortChanged"
      >
        <el-table-column width="55" align="center" fixed="left" label="序号">
          <template #default="scope">
            {{ pageable.pageSize * (pageable.pageNum - 1) + scope.$index + 1 }}
          </template>
        </el-table-column>
        <!-- 表格内容操作 -->
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="ts">
import {
  useTable,
  useDownload,
  useDictionary,
  useComputeTableHeight,
} from "@/utils/useTable";
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();
import { filterOrder } from "@/utils/commonHooks";
import dayjs from "dayjs";
import { Search } from "@element-plus/icons-vue";

defineOptions({
  name: "MESSTATISTICALDATAMODELPRODUCTIONREPORT",
  inheritAttrs: false,
});
// 目录树显隐
const showTree = ref(true);
const handelTree = () => {
  showTree.value = !showTree.value;
};
// 切换搜索折叠
const isExpanded = ref(false);
function toggleSearchArea() {
  isExpanded.value = !isExpanded.value;
}
// 请求参数
const requestApi = "/mes/messhapestatistics/queryByList";

// 是否需要分页
const pagination = true;
const initParam = reactive({
  assetKind: 1,
  sort: {
    column: "",
    orderBy: "",
  },
});

// 初始化搜索参数数据
const searchParamInit = {
  planDateSearch: [],
  planDateStart: "",
  planDateEnd: "",
  materialName: "",
  plannedBatch: "",
};

// 数据请求错误后处理
function requestError() {}

const tableHeight = ref();
function dataCallBack(data: any) {
  setTimeout(() => {
    const height1 =
      document.getElementById("descriptions-container")?.clientHeight + 10;
    tableHeight.value = useComputeTableHeight() - height1;
  }, 100);
  return data;
}

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError
);

onMounted(() => {
  getTableList();
  getTotalData();
});

function startSearch() {
  search();
  getTotalData();
}

function startReset() {
  reset();
  getTotalData();
}
// 排序
// 监听表格排序方法
function onTableSortChanged(column: any) {
  const { prop, order } = column;
  console.log(prop, order);

  initParam.sort = {
    column: prop,
    orderBy: filterOrder(order),
  };
}
// 监听页面 initParam 改化，重新获取表格数据
watch(() => initParam, getTableList, { deep: true });

const totalData = ref([]);
function getTotalData() {
  request({
    url: "/mes/messhapestatistics/queryTotalList",
    method: "post",
    data: toRaw(searchParam.value),
  }).then((res) => {
    if (res.code === 200) {
      totalData.value = res.data[0] ?? {};
      console.log(totalData.value);
    }
  });
}

// 处理时间
function getcreateTime(data: any) {
  if (data) {
    console.log(data);
    searchParam.value.planDateStart = data[0];
    searchParam.value.planDateEnd = data[1];
  } else {
    searchParam.value.planDateStart = "";
    searchParam.value.planDateEnd = "";
  }
}

// 接收 columns 并设置为响应式
const tableColumns = reactive([
  {
    prop: "planDate",
    label: "日期",
    minWidth: 110,
    isShow: true,
    align: "center",
    fixed: "left",
    render: (scope: any) => dayjs(scope.row.planDate).format("YYYY-MM-DD"),
  },
  {
    prop: "shiftName",
    label: "班次",
    minWidth: 130,
    isShow: true,
    align: "left",
    fixed: "left",
  },
  {
    prop: "materialCode",
    label: "料号",
    minWidth: 140,
    isShow: true,
    align: "left",
    fixed: "left",
  },
  {
    prop: "materialName",
    label: "品名",
    minWidth: 170,
    isShow: true,
    align: "left",
  },
  {
    prop: "plannedBatch",
    label: "批号",
    minWidth: 90,
    isShow: true,
    align: "center",
  },
  {
    prop: "pm",
    label: "P/M",
    minWidth: 60,
    isShow: true,
    align: "right",
  },
  {
    prop: "modelWeight",
    label: "模重",
    minWidth: 80,
    align: "right",
    isShow: true,
  },
  {
    prop: "singleWeight",
    label: "单重",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "planModulus",
    label: "计划模数",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "planWeight",
    label: "计划重量",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "planQuantity",
    label: "计划件数",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "planIronWeight",
    label: "计划铁水重量",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualModelMouldQuantity",
    label: "实际造型模数",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualPouringMouldQuantity",
    label: "实际浇注模数",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "modelAbandon",
    label: "造型废型",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "pouringAbandon",
    label: "浇注废型",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualWeight",
    label: "实际重量",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualQuantity",
    label: "实际件数",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualWaterWeight",
    label: "实际铁水重量",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "planProduceDuration",
    label: "计划生产时长\n(分钟)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualProduceDuration",
    label: "实际生产时长\n(分钟)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "packageWaitingTime",
    label: "计划包待\n(分钟)",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualPackage",
    label: "实际包待\n(分钟)",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "furnaceWaitingTime",
    label: "计划炉待\n(分钟)",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "actualStove",
    label: "实际炉待\n(分钟)",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "stoveNum",
    label: "炉号",
    minWidth: 80,
    isShow: true,
    align: "center",
  },
  {
    prop: "templateNum",
    label: "模板框号",
    minWidth: 80,
    isShow: true,
    align: "center",
  },
  {
    prop: "pressureOn",
    label: "压力(上)",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "pressureDown",
    label: "压力(下)",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandQuantityUp",
    label: "加砂量(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandQuantityDown",
    label: "加砂量(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould1Up",
    label: "砂模硬度1\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould1Down",
    label: "砂模硬度1\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould2Up",
    label: "砂模硬度2\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould2Down",
    label: "砂模硬度2\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould3Up",
    label: "砂模硬度3\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould3Down",
    label: "砂模硬度3\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould4Up",
    label: "砂模硬度4\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould4Down",
    label: "砂模硬度4\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould5Up",
    label: "砂模硬度5\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould5Down",
    label: "砂模硬度5\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould6Up",
    label: "砂模硬度6\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould6Down",
    label: "砂模硬度6\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould7Up",
    label: "砂模硬度7\n(上)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "sandMould7Down",
    label: "砂模硬度7\n(下)",
    minWidth: 96,
    isShow: true,
    align: "right",
  },
  {
    prop: "coreSettingUserName",
    label: "下芯人员",
    minWidth: 110,
    isShow: true,
    align: "center",
  },
  {
    prop: "sandCoreBatch",
    label: "砂芯批号",
    minWidth: 110,
    isShow: true,
    align: "center",
  },
  {
    prop: "sandCoreQuantity",
    label: "砂芯数量",
    minWidth: 80,
    isShow: true,
    align: "right",
  },
  {
    prop: "filterQuantity",
    label: "过滤片数量",
    minWidth: 90,
    isShow: true,
    align: "right",
  },
  {
    prop: "filterBatch",
    label: "过滤片批号",
    minWidth: 90,
    isShow: true,
    align: "right",
  },
]);

const searchColumns = ref([]);
</script>

<style lang="scss" scoped>
:deep(.el-table th.el-table__cell) {
  font-size: 13px;
  height: 48px;
  padding: 0 !important;
  .cell {
    line-height: 16px;
  }
}
:deep(.el-descriptions__header) {
  margin-bottom: 5px;
}
:deep(.el-descriptions__cell) {
  padding: 5px 7px !important;
  font-size: 13px !important;
}
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  //   max-width: 96px;
  max-width: 120px;
  min-width: 80px;
  //   color: #333;
}
// :deep(.el-card__body) {
//   padding-top: 12px;
// }
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 45px;
}
</style>
